package com.ibm.parse.dao;

import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;

public class TextParseDAO {
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return this.jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public List<Map<String, Object>> queryKeywordList() {
		String sql = "SELECT KIND,DIRECTION,PERIOD,RATE,AMOUNT,TYPE,GRADE,BOND,ACCEPT_BANK,BOND_TYPE,ISSUER_TYPE,ISSUER_NATURE,BOND_GRADE,BILL_TYPE,RISK_LEVEL,CURRENCY FROM KEYWORD";
		return this.jdbcTemplate.queryForList(sql);
	}

	public Map<String, Object> queryKeyword() {
		String sql = "SELECT KIND,DIRECTION,PERIOD,RATE,AMOUNT,TYPE,GRADE,BOND,ACCEPT_BANK,BOND_TYPE,ISSUER_TYPE,ISSUER_NATURE,BOND_GRADE,BILL_TYPE,RISK_LEVEL,CURRENCY FROM KEYWORD";
		Map map = this.jdbcTemplate.queryForMap(sql);

		map.put("acceptBank", map.get("ACCEPT_BANK"));
		map.put("bondType", map.get("BOND_TYPE"));
		map.put("issuerType", map.get("ISSUER_TYPE"));
		map.put("issuerNature", map.get("ISSUER_NATURE"));
		map.put("bondGrade", map.get("BOND_GRADE"));
		map.put("billType", map.get("BILL_TYPE"));
		map.put("riskLevel", map.get("RISK_LEVEL"));

		map.remove("ACCEPT_BANK");
		map.remove("BOND_TYPE");
		map.remove("ISSUER_TYPE");
		map.remove("ISSUER_NATURE");
		map.remove("BOND_GRADE");
		map.remove("BILL_TYPE");
		map.remove("RISK_LEVEL");

		return map;
	}

	public List<Map<String, Object>> queryKindIdentifierList() {
		String sql = "SELECT KIND,IDENTIFIER FROM KIND_IDENTIFIER";
		return this.jdbcTemplate.queryForList(sql);
	}

	public int update(String keywords) {
		String sql = "update KEYWORD set KIND=?, DIRECTION=?, TYPE=?, ACCEPT_BANK=?, GRADE=?, BOND=?, BOND_TYPE=?, ISSUER_TYPE=?, ISSUER_NATURE=?, BOND_GRADE=?, BILL_TYPE=?, RISK_LEVEL=?, CURRENCY=? ";

		JSONObject jo = JSONObject.fromObject(keywords);
		return this.jdbcTemplate.update(
				sql,
				new Object[] { jo.get("kind"), jo.get("direction"),
						jo.get("type"), jo.get("acceptBank"), jo.get("grade"),
						jo.get("bond"), jo.get("bondType"),
						jo.get("issuerType"), jo.get("issuerNature"),
						jo.get("bondGrade"), jo.get("billType"),
						jo.get("riskLevel"), jo.get("currency") });
	}
}